<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item label="公司名称" prop="companyName">
        <el-input v-model="dataForm.companyName" placeholder="公司名称"></el-input>
      </el-form-item>
      <el-row>
        <el-col :span="8">
          <el-form-item label="公司logo" prop="companyImg">
            <div class="xzcsretrt" v-if="visible">
              <dimg :dximg="dataForm.companyImg" @getimg="xzczxtrt"></dimg>
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="16">
          <el-form-item label="公司头图" prop="companyImg">
            <div class="xzcsretrt ab" v-if="visible">
              <dimg :dximg="dataForm.contentImg" @getimg="xzczxtrtb"></dimg>
            </div>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- <el-form-item label="省" prop="province">
        <el-input v-model="dataForm.province" placeholder="省"></el-input>
      </el-form-item>
          <el-form-item label="市" prop="city">
        <el-input v-model="dataForm.city" placeholder="市"></el-input>
      </el-form-item>
          <el-form-item label="县" prop="county">
        <el-input v-model="dataForm.county" placeholder="县"></el-input>
      </el-form-item> -->

      <el-row>
        <el-col :span="16">
          <el-form-item label="公司地址" prop="companyAddr">
            <div @click="jksncd = true" class="w100">
              <el-input readonly v-model="dataForm.companyAddr" placeholder="公司地址"></el-input>
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="企业类型" prop="companyAddr">
            <div class="w100">
              <el-radio-group v-model="dataForm.tag">
                <el-radio :label="item.value" v-for="item in jjxcf">{{ item.label }}</el-radio>
              </el-radio-group>
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="联系人">
            <el-input v-model="dataForm.linkMan" placeholder="联系人"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="联系人电话">
            <el-input v-model="dataForm.linkManTel" placeholder="联系人电话"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="推荐企业">
            <div class="w100">
              <el-radio-group v-model="dataForm.isGood">
                <el-radio :label="false">不推荐</el-radio>
                <el-radio :label="true">推荐</el-radio>
              </el-radio-group>
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="打卡范围">
            <div class="w100 display-flex">
              <div class="flex-1">
                <el-input v-model="dataForm.clock" placeholder="联系人打卡范围"></el-input>
              </div>
              <div class="vertical-center ml20">米</div>
            </div>
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="公司简介">
        <el-input type="textarea" rows="5" v-model="dataForm.content" placeholder="公司简介"></el-input>
      </el-form-item>
      <!-- <el-form-item label="联系人邮箱" prop="linkManEmail">
        <el-input v-model="dataForm.linkManEmail" placeholder="联系人邮箱"></el-input>
      </el-form-item>
          <el-form-item label="联系人职务" prop="linkManPosition">
        <el-input v-model="dataForm.linkManPosition" placeholder="联系人职务"></el-input>
      </el-form-item>
          <el-form-item label="备用联系人" prop="linkManTwo">
        <el-input v-model="dataForm.linkManTwo" placeholder="备用联系人"></el-input>
      </el-form-item>
          <el-form-item label="备用联系人电话" prop="linkManTwoTel">
        <el-input v-model="dataForm.linkManTwoTel" placeholder="备用联系人电话"></el-input>
      </el-form-item>
          <el-form-item label="备用联系人邮箱" prop="linkManTwoEmail">
        <el-input v-model="dataForm.linkManTwoEmail" placeholder="备用联系人邮箱"></el-input>
      </el-form-item>
          <el-form-item label="备用联系人职务" prop="linkManTwoPosition">
        <el-input v-model="dataForm.linkManTwoPosition" placeholder="备用联系人职务"></el-input>
      </el-form-item>
          <el-form-item label="初始用户密码" prop="initPwd">
        <el-input v-model="dataForm.initPwd" placeholder="初始用户密码"></el-input>
      </el-form-item> -->
      <!-- <el-form-item label="简介" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item> -->
      <!-- <el-form-item label="创建人" prop="createOwner">
        <el-input v-model="dataForm.createOwner" placeholder="创建人"></el-input>
      </el-form-item>
          <el-form-item label="创建时间" prop="createTime">
        <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
      </el-form-item>
          <el-form-item label="修改人" prop="updateOwner">
        <el-input v-model="dataForm.updateOwner" placeholder="修改人"></el-input>
      </el-form-item>
          <el-form-item label="修改时间" prop="updateTime">
        <el-input v-model="dataForm.updateTime" placeholder="修改时间"></el-input>
      </el-form-item> -->
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>

  <div class="cxzcxztrxtr" v-if="jksncd">
    <dxmap @close="jksncd = false" @success="kxczxret"></dxmap>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, nextTick } from "vue";
import baseService from "@/service/baseService";
import dimg from "@/components/dimg.vue";
import { ElMessage } from "element-plus";
const emit = defineEmits(["refreshDataList"]);
import dxmap from "@/components/dxmap.vue";
import { dxget, zkjsd, transferTimeYMD } from "@/utils/utils";
const xcxzdret = ref();
const visible = ref(false);
const dataFormRef = ref();
const uploadRef = ref();
const jjxcf = ref([]);
nextTick(() => {
  uploadRef.value.init();
});
const jksncd = ref();

const dataForm = reactive({
  id: "",
  companyCode: "",
  companyName: "",
  companyShortName: "",
  companyEnName: "",
  companyImg: "",
  contentImg: "",
  content: "",
  province: "",
  city: "",
  county: "",
  companyAddr: "",
  linkMan: "",
  linkManTel: "",
  linkManEmail: "",
  linkManPosition: "",
  linkManTwo: "",
  linkManTwoTel: "",
  linkManTwoEmail: "",
  linkManTwoPosition: "",
  initPwd: "",
  remark: "",
  createOwner: "",
  createTime: "",
  updateOwner: "",
  updateTime: ""
});

const kxczxret = (data) => {
  dataForm.companyAddr = data.dikjxzx;
  dataForm.lng = data.zb.split(",")[0];
  dataForm.lat = data.zb.split(",")[1];
  jksncd.value = false;
};

const rules = ref({
  companyCode: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  companyName: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  companyShortName: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  companyEnName: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  companyImg: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  contentImg: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  content: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  province: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  city: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  county: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  companyAddr: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  linkMan: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  linkManTel: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  linkManEmail: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  linkManPosition: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  linkManTwo: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  linkManTwoTel: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  linkManTwoEmail: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  linkManTwoPosition: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  initPwd: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  remark: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  createOwner: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  createTime: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  updateOwner: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  updateTime: [{ required: true, message: "必填项不能为空", trigger: "blur" }]
});

const xzczxtrt = (data) => {
  dataForm.companyImg = data;
};
const xzczxtrtb = (data) => {
  dataForm.contentImg = data;
};
const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";
  dataForm.contentImg = "";
  dataForm.companyAddr = "";
  dataForm.lng = "";
  dataForm.lat = "";
  dataForm.tag = "";
  dataForm.isGood = false;
  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    getInfo(id);
  }
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/api/tmcompany/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/sys/tmcompany", dataForm).then((res) => {
      ElMessage.success({
        message: "成功",
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
const getsd = async () => {
  jjxcf.value = [];
  const { list } = await zkjsd("1766718618927632385");
  list.map((a) => {
    jjxcf.value.push({
      label: a.dictLabel,
      value: a.dictValue
    });
  });
};
getsd();
</script>
<style scoped>
.cxzcxztrxtr {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  z-index: 9999;
}
</style>
